home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / ISSUE01 / CLINIC / TRIMSTR.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-05-15  |  2.0 KB  |  71 lines

  1. unit TrimStr;
  2. {$B-}
  3. {
  4.      File: TrimStr
  5.    Author: Bob Swart [100434,2072]
  6.   Purpose: routines for removing leading/trailing spaces from strings,
  7.            and to take parts of left/right of string (a la Basic).
  8.   Version: 2.0
  9.  
  10.   LTrim()    - Remove all spaces from the left side of a string
  11.   RTrim()    - Remove all spaces from the right side of a string
  12.   Trim()     - Remove all extraneous spaces from a string
  13.   RightStr() - Take a certain portion of the right side of a string
  14.   LeftStr()  - Take a certain portion of the left side of a string
  15.   MidStr()   - Take the middle portion of a string
  16.  
  17. }
  18. interface
  19. Const
  20.   Space = #$20;
  21.  
  22.   function LTrim(Const Str: String): String;
  23.   function RTrim(Str: String): String;
  24.   function Trim(Str: String):  String;
  25.   function RightStr(Const Str: String; Size: Word): String;
  26.   function LeftStr(Const Str: String; Size: Word): String;
  27.   function MidStr(Const Str: String; Size: Word): String;
  28.  
  29. implementation
  30.  
  31.   function LTrim(Const Str: String): String;
  32.   var len: Byte absolute Str;
  33.       i: Integer;
  34.   begin
  35.     i := 1;
  36.     while (i <= len) and (Str[i] = Space) do Inc(i);
  37.     LTrim := Copy(Str,i,len)
  38.   end {LTrim};
  39.  
  40.   function RTrim(Str: String): String;
  41.   var len: Byte absolute Str;
  42.   begin
  43.     while (Str[len] = Space) do Dec(len);
  44.     RTrim := Str
  45.   end {RTrim};
  46.  
  47.   function Trim(Str: String): String;
  48.   begin
  49.     Trim := LTrim(RTrim(Str))
  50.   end {Trim};
  51.  
  52.   function RightStr(Const Str: String; Size: Word): String;
  53.   var len: Byte absolute Str;
  54.   begin
  55.     if Size > len then Size := len;
  56.     RightStr := Copy(Str,len-Size+1,Size)
  57.   end {RightStr};
  58.  
  59.   function LeftStr(Const Str: String; Size: Word): String;
  60.   begin
  61.     LeftStr := Copy(Str,1,Size)
  62.   end {LeftStr};
  63.  
  64.   function MidStr(Const Str: String; Size: Word): String;
  65.   var len: Byte absolute Str;
  66.   begin
  67.     if Size > len then Size := len;
  68.     MidStr := Copy(Str,((len - Size) div 2)+1,Size)
  69.   end {MidStr};
  70. end.
  71.